
$(function(){
	
	$("#amDataTable").amDataGrid({url:window.rootPath + '/sysCfg/list',pagination:true,queryParams:$('#queryForm').amFormToJson()});
	
	
	$('#queryBtn').click(function(){
		$("#amDataTable").amDataGrid("load",$('#queryForm').amFormToJson());
	});
	
	$('#addBtn').bind('click',function(){
		location.href = window.rootPath + "/sysCfg/toEdit";
	});
	
	
	$("#batchDelBtn").click(function(){
		$("#amDataTable").amDataGrid("getSelectRows" , function(rows){
			 var ids = ''; 
			 for ( var i in rows) {
				ids += rows[i].id + ',';
			 }
			 ids = ids == '' ? '' : ids.substring(0,ids.length-1);
			 if(ids == '' ) return ;
			 var flag = confirm("确定要删除指定系统配置吗？"); 
			 if(flag){
			 	$.post(window.rootPath + '/sysCfg/batchDel', {ids:ids}, function(r) {
					if(r){
						$("#amDataTable").amDataGrid("refresh");
					}else{
						alert(r.m);
					}
			    }, 'json');
			 }
		});
	});
	
});


var formatter = { 
		
		status : function(value, data, index){
			if(value == '1'){
				return '<span class="am-text-success">正常</span>';
			}else{return '<span class="am-text-warning">禁用</span>'}
		},
		opt : function(value, data, index){
			var ohtml = ' <div class="am-btn-toolbar">'  +
						'	<div class="am-btn-group am-btn-group-xs"> ' +
						'   <button type="button" class="am-btn am-btn-default am-btn-xs am-text-secondary" onclick="configEdit('+index+')"><span class="am-icon-pencil-square-o"></span> 编辑</button> ' +
						'   <button type="button" class="am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only" onclick="delConfig('+index+')"><span class="am-icon-trash-o"></span> 删除</button> ' +
						'  </div></div>'
			return ohtml ;
		}
	};


function configEdit(index){
	var rows = $('#amDataTable').amDataGrid("getRows");
	console.info(rows[index].id);
	location.href = window.rootPath + "/sysCfg/toEdit?id=" + rows[index].id ;
}


function delConfig(index){
	var rows = $('#amDataTable').amDataGrid("getRows");
	var flag = confirm("确定要删除指定系统配置吗？"); 
	if(flag){
		$.post(window.rootPath + '/sysCfg/batchDel', {ids:rows[index].id}, function(r) {
			if(r){
				$("#amDataTable").amDataGrid("refresh");
			}else{
				alert(r.m);
			}
	    }, 'json');
	}
	
}